package com.qf.commons.web.config;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.qf.commons.core.exception.IExceptionListener;
import com.qf.commons.web.exception.ControllerExceptionHandler;
import com.qf.commons.web.exception.GlobalException;
import com.qf.commons.web.feign.FeignParamIntercetpor;
import com.qf.commons.web.feign.FeignParamterProperties;
import com.qf.commons.web.resp.ResponseDataHandler;
import com.qf.commons.web.sleuth.SleuthExceptionListener;
import com.qf.commons.web.utils.ApplicationUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * description: Web模块的自动装配的核心配置文件
 * author: Ken
 * 公众号：Java架构栈
 */
@Configuration
public class BaseConfiguration {

    /**
     * 相关基础、工具配置类
     */
    @Configuration
    public static class BaseAutoConfiguration{
        /**
         * 装配Application工具类
         * @return
         */
        @Bean
        public ApplicationUtils getApplicationUtils(){
            return new ApplicationUtils();
        }
    }

    /**
     * Web相关的配置依赖
     */
    @Configuration
    public static class WebMvcConfiguration{
        /**
         * 装配全局异常处理类
         * @return
         */
        @Bean
        public GlobalException getGlobalException(){
            return new GlobalException();
        }

        /**
         * 自定义的异常处理AOP
         * @return
         */
        @Bean
        public ControllerExceptionHandler exceptionHandler(){
            return new ControllerExceptionHandler();
        }

        /**
         * Http请求响应码的处理器（正确的设置Http请求的响应码）
         * @return
         */
        @Bean
        public ResponseDataHandler getResponseDataHandler(){
            return new ResponseDataHandler();
        }

        /**
         * 链路追踪的异常记录监听器
         * @return
         */
        @Bean
        public IExceptionListener getSleuthExceptionListern(){
            return new SleuthExceptionListener();
        }

        /**
         * 注册Web拦截器配置类
         * @return
         */
        @Bean
        public WebIntercepterConfig getWebIntercepterConfig(){
            return new WebIntercepterConfig();
        }
    }

    /**
     * nacos 注册中心相关的配置
     */
    @Configuration
    //启动Nacos的服务发现的功能
    @EnableDiscoveryClient
    public static class NacosConfiguration{

        /**
         * 配置相关的微服务的元数据数信息
         * @return
         */
        @Bean
        @Primary
        public NacosDiscoveryProperties getNacosDiscoverProperties(){
            NacosDiscoveryProperties nacosDiscoveryProperties = new NacosDiscoveryProperties();
            //设置相关的元数据信息
            Map<String, String> map = new HashMap<>();
            map.put("online.time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            nacosDiscoveryProperties.setMetadata(map);
            return nacosDiscoveryProperties;
        }
    }

    /**
     * Feign的相关配置
     */
    @Configuration
    @EnableConfigurationProperties(FeignParamterProperties.class)
    @EnableFeignClients(basePackages = "com.qf.business.feign", defaultConfiguration = FeignParamIntercetpor.class)
    public static class FeignConfiguration{

    }
}
